package com.xiangwei.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiangwei.dao.QrcodeMapper;
import com.xiangwei.dao.ScanRecordMapper;
import com.xiangwei.entity.ScanRecord;
import com.xiangwei.service.IScanrecordService;
import com.xiangwei.util.Common;
@Service
public class ScanrecordServiceImpl implements IScanrecordService {
	private static Logger logger = LoggerFactory.getLogger(ScanrecordServiceImpl.class);
	@Resource
	private ScanRecordMapper scanrecordMapper;
	@Resource
	private QrcodeMapper qrcodeMapper;
	
	@Override
	public int yesterScanrecordCount(Map<String, Object> map) {
		return scanrecordMapper.findScanrecordCountInDay(map);
	}

	@Override
	@Transactional
	public int insertScanrecord(Map<String, Object> map) {
		if(Common.isNullOrEmpty(map)){
			logger.error("method insertScanrecord 传入参数为空");
			return 0;			
		}
		String productId = (String) map.get("productId");
		String tableName = "qlm_portal_scanrecord_"+productId;
		int count = qrcodeMapper.existTable(tableName);
		if(count <= 0){
			qrcodeMapper.createScanrecordTable(productId);
		}
		return scanrecordMapper.insertScanrecord(map);
	}

	@Override
	@Transactional
	public ScanRecord selectByOpenid(String productId,String qrcodeId) {
//		System.out.println(openid);
//		System.out.println(productId);
//		System.out.println(qrcodeId);
		if(!Common.isEmptyString(productId) || !Common.isEmptyString(qrcodeId)){
			logger.error("method selectByOpenid 传入参数为空");
			return null;			
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("productId", productId);
//		map.put("openid", openid);
		map.put("qrcodeId", qrcodeId);
		String tableName = "qlm_portal_scanrecord_"+productId;
		int count = qrcodeMapper.existTable(tableName);
		System.out.println("count="+count);
		if(count <= 0){
			qrcodeMapper.createScanrecordTable(productId);
			return null;
		}
		return scanrecordMapper.selectByOpenid(map);
	}

	@Override
	@Transactional
	public Integer findScanCountByQrcode(String productId, String qrcodeId) {
		if(!Common.isEmptyString(productId) || !Common.isEmptyString(qrcodeId)){
			logger.error("method findfindScanCountByQrcode 传入参数为空");
			return null;			
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("productId", productId);
		map.put("qrcodeId", qrcodeId);
		String tableName = "qlm_portal_scanrecord_"+productId;
		int count = qrcodeMapper.existTable(tableName);
		if(count <= 0){
			qrcodeMapper.createScanrecordTable(productId);
			return null;
		}
		return scanrecordMapper.findScanCountByQrcode(map);
	}

	@Override
	public ScanRecord findRecordByScantime(String productId, String qrcodeId) {
		if(!Common.isEmptyString(productId) || !Common.isEmptyString(qrcodeId)){
			logger.error("method findfindScanCountByQrcode 传入参数为空");
			return null;			
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("productId", productId);
		map.put("qrcodeId", qrcodeId);
		String tableName = "qlm_portal_scanrecord_"+productId;
		int count = qrcodeMapper.existTable(tableName);
		if(count <= 0){
			qrcodeMapper.createScanrecordTable(productId);
			return null;
		}
		return scanrecordMapper.findRecordByScanTime(map);
	}

	@Override
	public Integer updateRecord(Map<String, Object> map) {
		if(Common.isNullOrEmpty(map)){
			logger.error("method updateRecord 传入参数为空");
			return 0;			
		}
		return scanrecordMapper.updateRecord(map);
	}

	@Override
	public int getScanrecordCount(Map<String, Object> map) {
		return scanrecordMapper.getScanrecordCount(map);
	}
	
	

}
